package com.matrix.draft.rabbitmqspringboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

/**
 * RabbitMQ(AMQP)相关配置
 */
//@Configuration
//@EnableRabbit
public class RabbitMQConfig {

    public static final String DELAYED_EXCHANGE = "DELAYED_EXCHANGE";
    public static final String DELAYED_ROUTING_KEY = "DELAYED_ROUTING_KEY";
    public static final String DEAD_QUEUE = "DEAD_QUEUE";

    // 声明一个延迟交换机
//    @Bean
//    Exchange delayedExchange2() {
//        return new CustomExchange(DELAYED_EXCHANGE,"x-delayed-message",true,false,new HashMap<String, Object>(){{
//            put("x-delayed-type","direct");
//        }});
//    }

    @Bean
    Exchange delayedExchange() {
        return ExchangeBuilder
                .directExchange(DELAYED_EXCHANGE)
                .delayed()
                .durable(true)
                .build();
    }


    // 声明一个死信队列
    @Bean
    Queue deadQueue() {
        return new Queue(DEAD_QUEUE);
    }

    // 声明一个绑定关系: 延时交换机&死信队列
    @Bean
    Binding bindingDeadQueue2DelayedExchange(@Qualifier("delayedExchange") Exchange delayedExchange,
                                             @Qualifier("deadQueue") Queue deadQueue) {
        return BindingBuilder.bind(deadQueue)
                .to(delayedExchange)
                .with(DELAYED_ROUTING_KEY)
                .noargs();
    }

}
